package com.ruoyi.kjcg.sbzn.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.kjcg.sbzn.domain.Sbzn;
import com.ruoyi.kjcg.sbzn.domain.SbznFj;
import com.ruoyi.kjcg.sbzn.service.ISbznService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 申报指南Controller
 *
 * @author huacai
 * @date 2024-11-07
 */
@RestController
@RequestMapping("/kjcg/sbzn")
public class SbznController extends BaseController {
    @Resource
    private ISbznService sbznService;

    /**
     * 查询申报指南列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:sbzn:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sbzn sbzn) {
        startPage();
        List<Sbzn> list = sbznService.selectSbznList(sbzn);
        return getDataTable(list);
    }

    /**
     * 导出申报指南列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:sbzn:export')")
    @Log(title = "申报指南", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sbzn sbzn) {
        List<Sbzn> list = sbznService.selectSbznList(sbzn);
        ExcelUtil<Sbzn> util = new ExcelUtil<Sbzn>(Sbzn.class);
        util.exportExcel(response, list, "申报指南数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Sbzn> util = new ExcelUtil<Sbzn>(Sbzn.class);
        util.importTemplateExcel(response, "申报指南数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "申报指南", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('kjcg:sbzn:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Sbzn> util = new ExcelUtil<Sbzn>(Sbzn.class);
        InputStream inputStream = file.getInputStream();
        List<Sbzn> list = util.importExcel(inputStream);
        inputStream.close();
        int count = sbznService.batchInsertSbzn(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取申报指南详细信息
     */
    @PreAuthorize("@ss.hasPermi('kjcg:sbzn:query')")
    @GetMapping(value = "/{sbznId}")
    public AjaxResult getInfo(@PathVariable("sbznId") String sbznId) {
        return success(sbznService.selectSbznBySbznId(sbznId));
    }

    /**
     * 新增申报指南
     */
    @PreAuthorize("@ss.hasPermi('kjcg:sbzn:add')")
    @Log(title = "申报指南", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sbzn sbzn) {
        sbzn.setUserId(Math.toIntExact(getUserId())); //用户ID
        sbzn.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        sbzn.setCreateBy(getUsername()); //创建人
        sbzn.setIdWhere("存在");
        return toAjax(sbznService.insertSbzn(sbzn));
    }

    /**
     * 修改申报指南
     */
    @PreAuthorize("@ss.hasPermi('kjcg:sbzn:edit')")
    @Log(title = "申报指南", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sbzn sbzn) {
        return toAjax(sbznService.updateSbzn(sbzn));
    }

    /**
     * 删除申报指南
     */
    @PreAuthorize("@ss.hasPermi('kjcg:sbzn:remove')")
    @Log(title = "申报指南", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sbznIds}")
    public AjaxResult remove(@PathVariable String[] sbznIds) {
        return toAjax(sbznService.deleteSbznBySbznIds(sbznIds));
    }

    /**
     * 获得申报指南主键ID (UUID)
     */
    @GetMapping("/getSbZnId")
    public AjaxResult getSbZnId() {
        String getSbZnId = IdUtils.fastSimpleUUID();
        return success(getSbZnId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertSbznFjBySbznId(@RequestBody SbznFj sbznFj) {
        return toAjax(sbznService.insertSbznFjBySbznId(sbznFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectSbznFjList(String sId) {
        SbznFj sbznFj = new SbznFj();
        sbznFj.setSbznId(sId);
        return AjaxResult.success(sbznService.selectSbznFjList(sbznFj));
    }

    /**
     * 删除申报指南附件
     */
    @Log(title = "申报指南附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteSbznFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(sbznService.deleteSbznFjByFjIds(fjIds));
    }

}
